package win.demonlegion.util;

import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 请求工具类, 通过RequestBean生出特定的访问
 */
@Slf4j
public class RequestUtil implements Serializable {
    private static final long serialVersionUID = 5725890420589401321L;

    public static String getUrl(String url, Object bean) {
        return getUrl(url, bean, null, bean.getClass());
    }

    public static String getUrl(String url, Object bean, boolean isEscape) {
        return getUrl(url, bean, null, bean.getClass(), isEscape);
    }

    public static String getUrl(String url, Object bean, String suffix) {
        return getUrl(url, bean, suffix, bean.getClass());
    }

    public static String getUrl(String url, Object bean, String suffix, boolean isEscape) {
        return getUrl(url, bean, suffix, bean.getClass(), isEscape);
    }

    public static <T> String getUrl(String url, Object bean, Class<T> clazz) {
        return getUrl(url, bean, null, clazz);
    }

    public static <T> String getUrl(String url, Object bean, Class<T> clazz, boolean isEscape) {
        return getUrl(url, bean, null, clazz, isEscape);
    }

    public static <T> String getUrl(String url, Object bean, String suffix, Class<T> clazz) {
        return getUrl(url, bean, null, suffix, clazz, true);
    }

    public static <T> String getUrl(String url, Object bean, String suffix, Class<T> clazz, boolean isEscape) {
        return getUrl(url, bean, null, suffix, clazz, isEscape);
    }

    public static <T> String getUrl(String url, Object bean, List<String> keyList, String suffix, Class<T> clazz, boolean isEscape) {
        if(url != null) {
            if(bean != null) {
                url += "?" + getParameters(bean, isEscape, keyList, clazz);
                // 增加url的后缀
                if(suffix != null) url += suffix;
                log.info(url);
                return url;
            }
        }
        return null;
    }

    public static String getParameters(Object bean, boolean isEscape) {
        return getParameters(bean, isEscape, bean.getClass());
    }

    public static <T> String getParameters(Object bean, boolean isEscape, Class<T> clazz) {
        return getParameters(bean, isEscape, null, clazz);
    }

    @SuppressWarnings("unchecked")
    public static <T> String getParameters(Object bean, boolean isEscape, List<String> keyList, Class<T> clazz) {
        StringBuilder parameters = new StringBuilder();
        if(bean != null) {
            Map<String, Object> params;
            if(bean instanceof Map) params = (Map<String, Object>) bean;
            else params = BeanMapUtil.beanToMap(bean, clazz);
            if(keyList == null) {
                keyList = params.keySet().stream().sorted().collect(Collectors.toList());
            }
            try {
                for(String key : keyList) {
                    if(params.get(key) != null) {
                        parameters.append(key).append("=").
                                append(isEscape ? URLEncoder.encode(params.get(key).toString(), "UTF-8") : params.get(key).toString())
                                .append("&");
                    }
                }
            } catch (UnsupportedEncodingException e) {
                log.error(e.toString());
            }
            // 删除最后的&符号
            if(parameters.length() > 0) parameters.deleteCharAt(parameters.length() - 1);
        }

        log.info(parameters.toString());
        return parameters.toString();
    }
}
